<template>
  <div>
    <a-drawer
      :title="title"
      :width="width"
      placement="right"
      :closable="false"
      @close="close"
      destroyOnClose
      :visible="visible">
      <mdm-customer-detail ref="realDetail" @ok="submitCallback" :disabled="disableSubmit" @edit="handleFormEdit"
                           @del="handleDelete" @tradeStatusOk="modalFormOk" normal></mdm-customer-detail>
      <div class="drawer-footer">
        <a-button @click="handleCancel" style="margin-bottom: 0;">关闭</a-button>
      </div>
    </a-drawer>
    <mdm-customer-modal ref="modalForm" @ok="modalFormOk"></mdm-customer-modal>
  </div>
</template>

<script>

  import MdmCustomerDetail from './MdmCustomerDetail'
  import MdmCustomerModal from './MdmCustomerModal'
  import {deleteAction, getAction} from '@/api/manage'

  export default {
    name: 'MdmCustomerModalDrawer',
    components: {
      MdmCustomerDetail,
      MdmCustomerModal
    },
    data () {
      return {
        title:"操作",
        width:896,
        visible: false,
        disableSubmit: false,
        url: {
          list: "/mdm/customer/list",
          delete: "/mdm/customer/delete",
        },
      }
    },
    methods: {
      add () {
        this.visible=true
        this.$nextTick(()=>{
          this.$refs.realDetail.add();
        })
      },
      edit (record) {
        this.visible=true
        this.$nextTick(()=>{
          this.$refs.realDetail.edit(record);
        });
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      submitCallback(){
        this.$emit('ok');
        this.visible = false;
      },
      handleOk () {
        this.$refs.realDetail.submitForm();
      },
      handleCancel () {
        this.close()
      },
      handleFormEdit: function (record) {
        this.$refs.modalForm.edit(record);
        this.$refs.modalForm.title = "编辑";
        this.$refs.modalForm.disableSubmit = false;
      },
      modalFormOk(id) {
        // 刷新详情页
        this.detailFresh(id);
        //刷新列表
        this.$emit('ok');
      },
      detailFresh(id){
        if(id){
          let params = {'id' : id};
          getAction(this.url.list,params).then((res)=>{
            if(res.success){
              let record = (res.result.records)[0];
              if(record){
                this.$refs.realDetail.headContent = record;
              }
            }else{
              this.$message.warning(res.message);
            }
          })
        }
      },
      handleDelete: function (id) {
        if(!this.url.delete){
          this.$message.error("请设置url.delete属性!")
          return
        }
        var that = this;
        deleteAction(that.url.delete, {id: id}).then((res) => {
          if (res.success) {
            //重新计算分页问题
            that.$message.success(res.message);
            that.visible = false;
          } else {
            that.$message.warning(res.message);
          }
        });

        that.$emit('ok');
      },
    }
  }
</script>

<style lang="less" scoped>
/** Button按钮间距 */
  .ant-btn {
    margin-left: 30px;
    margin-bottom: 30px;
    float: right;
  }
  .drawer-footer{
    position: absolute;
    bottom: -8px;
    width: 100%;
    border-top: 1px solid #e8e8e8;
    padding: 10px 16px;
    text-align: right;
    left: 0;
    background: #fff;
    border-radius: 0 0 2px 2px;
  }

</style>